//
//  SSDKBaseReqData.m
//  SuperSDK
//
//  Created by wyht－ios－dev on 15/10/7.
//  Copyright © 2015年 WYHT. All rights reserved.
//

#import "SSDKBaseReqData.h"
#import "SuperSDKInst.h"
#import "SSDKDataKeys.h"
#import "SuperSDKPluginTools/SSDKPluginJsonUtil.h"

@implementation SSDKBaseReqData

- (instancetype) init {
    self = [super init];
    
    if (self) {
        self.data = [[NSMutableDictionary alloc] init];
        self.common = [[NSMutableDictionary alloc] init];
        self.extra = [[NSMutableDictionary alloc] init];
        
        //初始化公用的common信息
        self.common = [[SuperSDKInst getInstance] getClientInfo];
    }
    
    return self;
}

- (NSString *) appId
{
    NSString *value = [self.data valueForKey:APP_ID];
    if (!value) {
        return @"";
    }
    return value;
}

- (instancetype) setAppId:(NSString *)appId
{
    [self.data setValue:appId
                 forKey:APP_ID];
    return self;
}

- (NSString *) sdkId
{
    NSString *value = [self.data valueForKey:SDK_ID];
    if (!value) {
        return @"";
    }
    return value;
}

- (instancetype) setSdkId:(NSString *)sdkId
{
    [self.data setValue:sdkId
             forKey:SDK_ID];
    return self;
}

- (NSString *) channelId
{
    NSString *value = [self.data valueForKey:CHANNEL_ID];
    if (!value) {
        return @"";
    }
    return value;
}

- (instancetype) setChannelId:(NSString *)channelId
{
    [self.data setValue:channelId
             forKey:CHANNEL_ID];
    return self;
}

-(NSString *) versionName
{
    NSString *value = [self.data valueForKey:VERSION_NAME];
    if (!value) {
        return @"";
    }
    return value;
}

-(instancetype) setVersionName:(NSString *) versionName
{
    [self.data setValue:versionName
                 forKey:VERSION_NAME];
    return self;
}

-(int) versionCode
{
    NSString *value = [self.data valueForKey:VERSION_CODE];
    if (!value) {
        return 0;
    }
    return [value intValue];
}

-(instancetype) setVersionCode:(int)versionCode
{
    [self.data setValue:[NSString stringWithFormat:@"%d",versionCode]
                 forKey:VERSION_CODE];
    return self;
}

- (instancetype) setSign:(NSString *)sign
{
    [self.data setValue:sign
                 forKey:SIGN];
    return self;
}

- (NSString *) sign {
    return [self dataWithKey:SIGN];
}

- (NSString *) dataWithKey:(NSString *)key
{
    NSString *value = [self.data valueForKey:key];
    if (value == nil) {
        return @"";
    }
    return value;
}

- (instancetype) setDataValue : (NSString *) value
                       forKey : (NSString *) key
{
    [self.data setValue:value forKey:key];
    return self;
}

- (NSString *) commonWithKey:(NSString *)key
{
    NSString *value = [self.common valueForKey:key];
    if (value == nil) {
        return @"";
    }
    return value;
}

- (instancetype) setCommonValue : (NSString *) value
                       forKey : (NSString *) key
{
    [self.common setValue:value forKey:key];
    return self;
}

- (NSString *) extraWithKey:(NSString *)key
{
    NSString *value = [self.extra valueForKey:key];
    if (value == nil) {
        return @"";
    }
    return value;
}

- (instancetype) setExtraValue:(NSString *)value
                        forKey:(NSString *)key
{
    [self.extra setValue:value forKey:key];
    return self;
}

- (NSString *) toJson
{
    NSMutableDictionary *jsonDic = [[NSMutableDictionary alloc] init];
    
    UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000;
    NSString *timeSp = [NSString stringWithFormat:@"%lld", recordTime];
    
    [jsonDic setValue:timeSp forKey:TIME];
    [jsonDic setValue:self.data forKey:DATA];
    [jsonDic setValue:self.extra forKey:EXTRA];
    [jsonDic setValue:self.common forKey:COMMON];
    return [SSDKPluginJsonUtil toJsonWithObject:jsonDic];
}

@end
